package javase.day05;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class Demo2 {
	public static void main(String[] args){
		String file = "/home/tarena/cp.txt";
		read(file);
		List<User> users = readUser(file);
		System.out.println(users);
	}
	
	public static List<User> readUser(String file){
		try {
			BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file),"gbk"));
			List<User> users = new ArrayList<User>();
			String line;
			while((line=in.readLine())!=null){
				line = line.trim();
				if(line.equals("")|| line.startsWith("#")){
					continue;
				}
				User user = parseUser(line);
				users.add(user);
			}
			return users;
			//in.close();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
	}
	
	public static User parseUser(String str){
		String[] data = str.split("\\|\\s*");
		User user = new User();
		user.setId(Integer.parseInt(data[0]));
		user.setName(data[1]);
		user.setEmail(data[2]);
		user.setMobile(data[3]);
		user.setQq(data[4]);
		return user;
	}
	
	public static void read(String file){
		try {
			BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file),"gbk"));
			String line;
			while((line=in.readLine())!=null){
				System.out.println(line);
			}
			in.close();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException();
		}catch(IOException e){
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
}
class User{
	int id ;
	String name;
	String email;
	String mobile;
	String qq;
	public User(){}
	public User(int id, String name, String email, String mobile, String qq) {
		super();
		this.id = id;
		this.name = name;
		this.email = email;
		this.mobile = mobile;
		this.qq = qq;
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getMobile() {
		return mobile;
	}
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
	public String getQq() {
		return qq;
	}
	public void setQq(String qq) {
		this.qq = qq;
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return this.name+","+this.email+","+this.qq+","+this.mobile+"\n";
	}
	
}






